<template>
  <div class="app-container">
    <el-row :gutter="20">
      <leftPanel treeType="chapter" @leftClick="getTreeNotes" />
      <el-col :span="20" :xs="24">
        <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
          <el-form-item label="考核方案" prop="paperName">
            <el-input v-model="queryParams.paperName" placeholder="请输入考核方案" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!--      <el-form-item label="题数" prop="generateQuestionNum">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.generateQuestionNum"-->
          <!--          placeholder="请输入题数"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
          <el-form-item label="份数" prop="generateNum">
            <el-input v-model="queryParams.generateNum" placeholder="请输入份数" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="范围类型" prop="generateScopeType">
            <el-select v-model="queryParams.generateScopeType" placeholder="请输入范围类型" clearable @keyup.enter.native="handleQuery">
              <el-option v-for="item in scopeTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="院系" prop="departName">
            <el-input v-model="queryParams.departName" placeholder="请输入院系" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="专业" prop="majorName">
            <el-input v-model="queryParams.majorName" placeholder="请输入专业" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="题库" prop="courseSubject">
            <el-input v-model="queryParams.courseSubject" placeholder="请输入题库" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="创建人" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入创建人" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!--          <el-form-item label="创建时间" prop="createdTime">-->
          <!--            <el-date-picker v-model="queryParams.createdTime" clearable type="date" value-format="yyyy-MM-dd"-->
          <!--                            placeholder="请选择创建时间">-->
          <!--            </el-date-picker>-->
          <!--          </el-form-item>-->
          <!--      <el-form-item label="更新人" prop="updatedBy">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.updatedBy"-->
          <!--          placeholder="请输入更新人"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
          <!--      <el-form-item label="更新时间" prop="updatedTime">-->
          <!--        <el-date-picker clearable-->
          <!--          v-model="queryParams.updatedTime"-->
          <!--          type="date"-->
          <!--          value-format="yyyy-MM-dd"-->
          <!--          placeholder="请选择更新时间">-->
          <!--        </el-date-picker>-->
          <!--      </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['juan:juanGenerate:add']"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              :disabled="!queryParams.courseId"
              @click="handleAdd"
              >自动组卷</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['juan:juanGenerate:add']"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              :disabled="!queryParams.courseId"
              @click="handleManualAdd"
              >人工组卷</el-button
            >
          </el-col>
          <!--      <el-col :span="1.5">-->
          <!--        <el-button-->
          <!--          v-hasPermi="['juan:juanGenerate:edit']"-->
          <!--          type="success"-->
          <!--          plain-->
          <!--          icon="el-icon-edit"-->
          <!--          size="mini"-->
          <!--          :disabled="single"-->
          <!--          @click="handleUpdate"-->
          <!--          >修改</el-button-->
          <!--        >-->
          <!--      </el-col>-->
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['juan:juanGenerate:remove']"
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              >删除</el-button
            >
          </el-col>
          <!--      <el-col :span="1.5">-->
          <!--        <el-button-->
          <!--          v-hasPermi="['juan:juanGenerate:export']"-->
          <!--          type="warning"-->
          <!--          plain-->
          <!--          icon="el-icon-download"-->
          <!--          size="mini"-->
          <!--          @click="handleExport"-->
          <!--          >导出</el-button-->
          <!--        >-->
          <!--      </el-col>-->
          <!--      <right-toolbar-->
          <!--        :show-search.sync="showSearch"-->
          <!--        @queryTable="getList"-->
          <!--      ></right-toolbar>-->
        </el-row>

        <el-table v-loading="loading" :data="juanGenerateList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="考核方案" align="center" prop="paperName" />
          <!--      <el-table-column label="题型" align="center" prop="generateQuestionType" />-->
          <!--      <el-table-column label="题数" align="center" prop="generateQuestionNum" />-->
          <el-table-column label="份数" align="center" prop="generateNum" />
          <!--      <el-table-column label="规则" align="center" prop="generateRule" />-->
          <el-table-column label="难度系数" align="center" prop="generateEasy">
            <template slot-scope="scope">
              {{ scope.row.generateEasy | floatFormat }}
            </template>
          </el-table-column>
          <el-table-column label="重复率" align="center" prop="generateRepeat">
            <template slot-scope="scope">
              {{ scope.row.generateRepeat | floatFormat }}
            </template>
          </el-table-column>
          <!--      <el-table-column label="范围" align="center" prop="generateScope" />-->
          <!--          <el-table-column label="是否有密封线" align="center" prop="paperSealLine">-->
          <!--            <template slot-scope="scope">-->
          <!--              <span v-if="scope.row.paperSealLine === '0'">否</span>-->
          <!--              <span v-else-if="scope.row.paperSealLine === '1'">是</span>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="考试时长" align="center" prop="paperTime" />
          <el-table-column label="试卷总分" align="center" prop="paperTotalScore" />
          <el-table-column label="范围类型" align="center" prop="generateScopeType">
            <template slot-scope="scope">
              <span v-if="scope.row.generateScopeType === '1'">章节选题</span>
              <span v-else-if="scope.row.generateScopeType === '2'">知识点选题</span>
              <span v-else-if="scope.row.generateScopeType === '3'">试卷选题</span>
            </template>
          </el-table-column>
          <el-table-column label="注意事项" align="center" prop="paperAttention" />
          <el-table-column label="院系" align="center" prop="departName" />
          <el-table-column label="专业" align="center" prop="majorName" />
          <el-table-column label="题库" align="center" prop="courseSubject" />
          <el-table-column label="创建人" align="center" prop="userName" />
          <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdTime, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <!--      <el-table-column label="更新人" align="center" prop="updatedBy" />-->
          <!--      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">-->
          <!--        <template slot-scope="scope">-->
          <!--          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>-->
          <!--        </template>-->
          <!--      </el-table-column>-->
          <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-s-promotion" @click="createPaper(scope.row)">生成试卷 </el-button>
              <el-button
                v-hasPermi="['juan:juanGenerate:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改
              </el-button>
              <el-button
                v-hasPermi="['juan:juanGenerate:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改组卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="份数" prop="generateNum">
          <el-input-number v-model="form.generateNum" :precision="0" :min="1" :max="10000" style="width: 120px"></el-input-number>
        </el-form-item>
        <el-form-item v-if="form.generateNum > 1" label="重复率" prop="generateRepeat">
          <el-input-number
            v-model="form.generateRepeat"
            :precision="2"
            :step="0.01"
            :min="0"
            :max="0.99"
            style="width: 120px"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addJuanGenerate, autoGeneratePaper, delJuanGenerate, listJuanGenerate, updateJuanGenerate } from "@/api/juan/juanGenerate";
import { getKnowledgeTreeByQuestion, getKnowledgeTreeByQuestion1 } from "@/api/juan/juanQuestion";

export default {
  name: "JuanGenerate",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组卷表格数据
      juanGenerateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        generateId: undefined,
        generateType: undefined,
        generateQuestionType: undefined,
        generateQuestionNum: undefined,
        generateNum: undefined,
        generateRule: undefined,
        generateEasy: undefined,
        generateRepeat: undefined,
        generateScope: undefined,
        generateScopeType: undefined,
        createdBy: undefined,
        createdTime: undefined,
        updatedBy: undefined,
        updatedTime: undefined,
        userName: undefined,
        paperName: undefined,
        paperTime: undefined,
        paperTotalScore: undefined,
        paperSealLine: undefined,
        paperAttention: undefined,
        departName: undefined,
        majorName: undefined,
        courseSubject: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        generateId: [{ required: true, message: "租户号不能为空", trigger: "blur" }],
        generateType: [{ required: true, message: "类型不能为空", trigger: "change" }],
        generateQuestionType: [{ required: true, message: "题型不能为空", trigger: "change" }],
        generateQuestionNum: [{ required: true, message: "题数不能为空", trigger: "blur" }],
        generateNum: [{ required: true, message: "份数不能为空", trigger: "blur" }],
        generateRule: [{ required: true, message: "规则不能为空", trigger: "blur" }],
        generateEasy: [{ required: true, message: "难度系数不能为空", trigger: "blur" }],
        generateRepeat: [{ required: true, message: "重复率不能为空", trigger: "blur" }],
        generateScope: [{ required: true, message: "范围不能为空", trigger: "blur" }],
        createdBy: [{ required: true, message: "创建人不能为空", trigger: "blur" }],
        createdTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
        updatedBy: [{ required: true, message: "更新人不能为空", trigger: "blur" }],
        updatedTime: [{ required: true, message: "更新时间不能为空", trigger: "blur" }],
      },
      isRouter: false,
      scopeTypeOptions: [
        {
          value: "2",
          label: "知识点选题",
        },
        {
          value: "3",
          label: "试卷选题",
        },
      ],
    };
  },
  created() {},
  methods: {
    // 树节点单击事件
    getTreeNotes(obj) {
      if (obj) {
        this.queryParams.courseId = obj.treeNode.id;
        this.handleQuery();
      } else {
        this.queryParams.courseId = undefined;
        this.juanGenerateList = [];
      }
      this.loadQuestionOption();
    },

    loadQuestionOption() {
      if (!this.queryParams.courseId) {
        return;
      }
      getKnowledgeTreeByQuestion1({
        courseId: this.queryParams.courseId,
      })
        .then((res) => {
          const data = res.data;
          this.isRouter = false;
          if (data && data.length > 0) {
            this.isRouter = true;
          }
        })
        .catch();
    },

    /** 查询组卷列表 */
    getList() {
      this.loading = true;
      listJuanGenerate(this.queryParams)
        .then((response) => {
          this.juanGenerateList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        generateId: undefined,
        generateType: undefined,
        generateQuestionType: undefined,
        generateQuestionNum: undefined,
        generateNum: undefined,
        generateRule: undefined,
        generateEasy: undefined,
        generateRepeat: undefined,
        generateScope: undefined,
        createdBy: undefined,
        createdTime: undefined,
        updatedBy: undefined,
        updatedTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.generateId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      //   this.reset();
      //   this.open = true;
      //   this.title = "添加组卷";
      if (this.isRouter) {
        this.$router.push({
          path: "/juanGenerate/add",
          query: {
            courseId: this.queryParams.courseId,
          },
        });
      } else {
        this.$modal.msgError("该课程暂无知识点内容");
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: "/juanGenerate/edit",
        query: {
          generateId: row.generateId,
          courseId: this.queryParams.courseId,
        },
      });
      // this.loading = true;
      // this.reset();
      // const generateId = row.generateId || this.ids;
      // getJuanGenerate(generateId).then((response) => {
      //   this.loading = false;
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改组卷";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.generateId != null) {
            updateJuanGenerate(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addJuanGenerate(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const generateIds = row.generateId || this.ids;
      this.$modal
        .confirm("是否确认删除组卷？")
        .then(() => {
          this.loading = true;
          return delJuanGenerate(generateIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "juan/juanGenerate/export",
        {
          ...this.queryParams,
        },
        `juanGenerate_${new Date().getTime()}.xlsx`
      );
    },
    handleManualAdd() {
      this.$router.push("/juanPaper/juanPaper/add");
    },
    createPaper(data) {
      console.log(data);
      autoGeneratePaper({
        generateId: data.generateId,
      })
        .then((res) => {
          this.$modal.msgSuccess("组卷成功");

          setTimeout(() => {
            this.$router.push("/juanGenerate/juanPaper/index");
          }, 1000);
        })
        .catch();
    },
  },
};
</script>
